Ext.BLANK_IMAGE_URL = basePath + 'ext3/resources/images/default/s.gif';
Ext.SSL_SECURE_URL = basePath + 'ext3/resources/images/default/s.gif';
//author: xuyan
//统一编码相关
Ext.tybm = function(){
	var clsCommUrl = basePath + 'JarpListener?className=cn.com.jsepc.midawa.utils.CommUtils';
	var thisMethod = '&methodName=getTybmxx';
	var tybmReader = new Ext.data.JsonReader({}, ['tcode', 'tname']);
	return{
		/*  ----------------------------------------------------------
			统一编码自动获取URL, TreePanle 或者 ComboBox 通过dataUrl加载数据
			只需组织好统一编码查询参数后传入即可,.eg
			node.loader = new Ext.tree.TreeLoader({
				dataUrl: Ext.tybm.getAutoUrl('auto=1&ctid=3&tree=1&async=1')
			});
		    ----------------------------------------------------------  */
		getAutoUrl: function(params){
			var paraObj = [Ext.urlDecode(params)];
			var msg = '';
			if(typeof params != 'string'){
				msg += '参数类型错误';
			}
			if(typeof paraObj[0].auto == 'undefined'){
				msg += (msg != ''?'<br>':'') + "参数定义错误, 缺少auto参数";
			}
			if(msg != ''){
				Ext.Msg.alert('统一编码 - getAutoUrl', msg, null);
				return '';
			}
			return clsCommUrl + thisMethod + '&paramCount=1&param_0=' + Ext.encode([Ext.urlDecode(params)]);
		},

		/*  ----------------------------------------------------------
			获取统一编码Store的reader,.eg
			var storeZblb = new Ext.data.Store({
				reader:Ext.tybm.getReader()
			});
			----------------------------------------------------------  */
		getReader: function(){
			return tybmReader;
		},
		
		/*  ----------------------------------------------------------
			获取统一编码, 传入查询参数以及回调函数, 回调函数接受的参数为http对象
			返回的responseText
			----------------------------------------------------------  */
		getTheseTybm: function(params, callback){
			//验证params以及Http对象
			var msg = '';
			if(typeof params != 'object')
				msg += "参数类型错误, 类型为" + typeof params;
			if(params.length == 0)
				msg += (msg != ''?'<br>':'') + "参数长度错误, 长度为0";
			if(msg != ''){
				//alert(msg);
				Ext.Msg.alert('统一编码 - getTheseTybm', msg, null);
				return;
			}
			//生成参数object
			var pArr = [];
			for(var i=0; i<params.length; i++){
				pArr[i] = Ext.urlDecode(params[i]);
			}
			//Send Request
			var thisUrl = clsCommUrl + thisMethod + '&paramCount=0';
			Ext.http.send(thisUrl, function(rtnTxt){callback(Ext.decode(rtnTxt));}, Ext.encode(pArr));
		}
}}()

//数据传递相关
Ext.http = function(){
	var httpIndex = 0;
	return{
		/*  ----------------------------------------------------------
			获取http请求对象, 获取失败返回false
			方法来源:XMLHttpRequest.js getXMLHttpRequest方法
			包含这个方法可以省去jsp页面include XMLHttpRequest.js 文件, 更加集成化
			----------------------------------------------------------  */
		getHttpReqObj:function(){
			var httpReq="";
			if(window.XMLHttpRequest){
				httpReq=new XMLHttpRequest();
				if(httpReq.overrideMimeType){
		              httpReq.overrideMimeType("text/xml");
				}
			}else if(window.ActiveXObject){
				try{
					httpReq=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e){
					try{
						httpReq=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e){}
				}
			}
			if(!httpReq){
				return false;
			}
			return httpReq;
		},
		
		/*  ----------------------------------------------------------
			发送请求到后台并在callback中进行返回处理, sendParams是不在url中的参数,
			在调用时组织, json格式的可用Ext.encode(pArr)传入
			----------------------------------------------------------  */
		send:function(thisUrl, callback, sendParams){
			//if(sendParams == null){									//禁止传递null参数
			//	Ext.Msg.alert('数据传递 - send', formatMsg('Http参数类型错误, 不能为null值, 请输入字符型'), null);
			//	return;
			//}
			if(typeof sendParams == 'undefined')						//不填sendParams参数的情况
				sendParams = null;
			if(sendParams != null && typeof sendParams != 'string'){	//填了sendParams参数, 要保证类型正确
				Ext.Msg.alert('数据传递 - send', formatMsg('Http参数类型错误, 不能为' + typeof sendParams + '型, 请输入字符型'), null);
				return;
			}
			if(typeof sendParams == 'string' && sendParams.length == 0){//填了string型参数, 要保证参数长度不能为0; 可以尝试封装object方式, decode传送
				Ext.Msg.alert('数据传递 - send', formatMsg('Http参数值错误, 长度不能为0'), null);
				return;
			}
			var thisIndex = httpIndex++;
			var httpReqName = 'thisHttpReq' + thisIndex;
			var httpCallback = 'thisCallback' + thisIndex;
			var httpReqRtn = 'thisHttpRtn' + thisIndex;
			
			eval(httpReqName + ' = this.getHttpReqObj();');
			// ---- add these marked lines if validation required ----
			//if(eval(httpReqName + '!=false;')){
				eval(httpCallback + ' = callback');
				eval(httpReqRtn + '=function(){if (' + httpReqName + '.readyState == 4) {if (' + httpReqName + '.status == 200) {' + httpCallback + '(' + httpReqName + '.responseText);' + httpReqName + '=null;}}}');
				//alert(eval(httpReqRtn));
				//指定http返回处理函数
				eval(httpReqName + '.onreadystatechange = ' + httpReqRtn + ';');
				eval(httpReqName + '.open("POST", thisUrl, true);');
				eval(httpReqName + '.send(sendParams);');
			//}else{
			//	Ext.Msg.alert('提示', '获取Http请求对象失败', null);
			//}
		},
		
		/*  ----------------------------------------------------------
			http对象使用情况统计, 叫做pool, 貌似对象池, 其实不是
			----------------------------------------------------------  */
		pool:function(){
			var msg = 'there are ' + httpIndex + ' HttpRequest Objects in the pool:';
			for(var i=0; i<httpIndex; i++){
				msg += ('<br>' + i + ':') + eval('thisHttpReq' + i);
			}
			Ext.Msg.alert('数据传递 - pool', msg, null);
		}
	}
}()

//Tree操作相关相关
Ext.mytree = function(){
	return{
		/*  ----------------------------------------------------------
			将JSON Object数据递归填入TreePanel的某一node中
			----------------------------------------------------------  */
		fillTree: function(thisNode, theseData){
			for(var ino=0; ino<theseData.length; ino++){
				var newNode = new Ext.tree.TreeNode(theseData[ino]);
				thisNode.appendChild(newNode);
				if(!newNode.isLeaf()){
					this.fillTree(newNode, theseData[ino].children);
				}
			}
		},

		/*  ----------------------------------------------------------
			删除thisNode节点下的所有子结点, eg.想删除treepanel中根节点下的所有节点
			Ext.mytree.clearSubNodes(myTreePnl.getRootNode());
			----------------------------------------------------------  */
		clearSubNodes:function(thisNode){
			var subNodes = thisNode.childNodes;
			for(var i=subNodes.length - 1; i>=0; i--){
				subNodes[i].remove();
			}
		},
		
		resetBoldNodes:function(thisNode){
			if(typeof thisNode != 'object' || thisNode == null)
				return;
			thisNode.getUI().removeClass('selitem');
			for(var ic=0; ic<thisNode.childNodes.length; ic++){
				thisNode.childNodes[ic].getUI().removeClass('selitem');
				if(thisNode.childNodes[ic].childNodes.length > 0)
					this.resetBoldNodes(thisNode.childNodes[ic]);
			}
		}
	}
}()

//fusion charts 操作相关
Ext.chart = function(){
	var swfUrl = {
		column2d:	"target/charts/Column2D.swf",
		line:		"target/charts/Line.swf",
		pie2d:		"target/charts/Pie2D.swf",
		mscolumn2d:	"target/charts/MSColumn2D.swf",
		msline:		"target/charts/MSLine.swf"
	};
	
	//ComboBox中 valueField为value, displayField为text
	var stChart = new Ext.data.SimpleStore({
		fields:['value','text'],
		data:[['grid',		'表格'],
			  ['Column2D',	'柱状图'],
			  ['Line',		'折线图'],
			  ['Pie2D',		'饼图'],
			  ['MSColumn2D','多维柱状图'],
			  ['MSLine',	'多维折线图']]
	});
	return{
		/*  ----------------------------------------------------------
			Combox的Store使用Ext.chart.getChartStore
			valueField 使用 value, displayField 使用 text
			----------------------------------------------------------  */
		getChartStore:function(){
			return stChart;
		},

		/*  ----------------------------------------------------------
			获取根据swf类型获取swf地址, 使用basePath, 可以手工传入bsPath, 如果
			没有传入, 使用全局的basePath, 如果两个值都没有提供, 返回空地址
			----------------------------------------------------------  */
		getSwfUrl:function(swfCode, bsPath){
			if(bsPath == undefined && basePath == undefined){
				Ext.Msg.alert('图表操作 - getSwfUrl', formatMsg('未提供basePath, 请检查', 2), null);
				return '';
			}
			try{
				eval('swfUrl.' + swfCode.toLowerCase())
			}catch(e){
				Ext.Msg.alert('图表操作 - getSwfUrl', formatMsg('图形代码' + swfCode + '错误, 请检查', 2), null);
				return '';
			}
			return (bsPath==undefined?basePath:bsPath) + eval('swfUrl.' + swfCode.toLowerCase());
		}

		/*  ----------------------------------------------------------
			根据选择的swf类型, 重新加载数据,传入chart所在的panel.
			默认param_0携带swf类型值, 作为保留参数
			----------------------------------------------------------  */
		//,loadFlash:function(thisPnl, swfCode){
		//	thisPnl.swf = this.getSwfUrl(swfCode);
		//	thisPnl.flashvars.dataUrl = setUrlParam(thisPnl.flashvars.dataUrl, 'param_0', swfCode);
		//	thisPnl.loadFlash();
		//}
	}
}()
//setTimeout(function(){Ext.http.send(basePath + 'JarpListener?className=cn.com.jsepc.midawa.utils.CommUtils&methodName=getTybmxx&paramCount=1&param_0=3', function(rtnTxt){});}, null);
//setTimeout(function(){Ext.http.pool();},1000);

//将字符转为UNICODE编码形式, 反向函数为unescape
function unicodeThis(str){
	var Code;
	var UnicodeStr="";
	for(i=0;i<str.length;i++){
		Code=(str.charCodeAt(i)).toString(16);
		Code="0000".substring(0,4-Code.length)+Code;
		UnicodeStr+="%u"+Code;
	}
	return UnicodeStr;
}
formatMsg = function(msg, tens){
	if(typeof msg != 'string')
		msg = '';
	var slen = 10 * (typeof tens=='undefined'?1:tens);
   	if(msg.length < slen){
   		var len = slen - msg.length;
   		for(var i = 0; i < len; i++){
   			msg = ('&nbsp;' + msg + '&nbsp;');
   		}
	}
	return msg;
}
function po(obj, property, func){
  //eval(obj.functionname);
  var str = '';
  for(prop in obj){
    if(typeof(obj[prop]) != 'function'){
    	if(property != false){
    		str += prop + ':' + obj[prop] + '\n';
    	}
    }
    else if(func != false){
    	str += prop + ':' + typeof(obj[prop]) + '\n';
    }
  }
  return str;
}

function po2(obj, property, func){
  if(typeof obj == 'undefined')
	return;
  try{
    var str = '', id = '';
	if(typeof obj == 'object'){
		for(prop in obj){
		  if(typeof(obj[prop]) != 'function'){
		  	if(property != false){
		  		str += prop + ':' + obj[prop] + '\n';
		  	}
		  }
		  else if(func != false){
		  	str += prop + ':' + typeof(obj[prop]) + '\n';
		  }
		}
		id = 'object:' + (obj.id == undefined?'':obj.id);
	 }else if(typeof obj == 'string'){
	 	str = obj;
	 	id = 'string';
	 }else if(typeof obj == 'function'){
	 	str = eval(obj);
	 	id = 'function';
	 }
	 
	 if(typeof Ext != 'undefined'){
		new Ext.Window({
		  	title:id,
		  	width:500,
		  	height:350,
		  	collapsible:true,
		  	layout:'fit',
		  	items:{ xtype: 'textarea',
		            value:str
				}
		  }).show();
	 }else{
		alert(str);
	 }
  }catch(e){
  	
  }
  //str.replace(/\n/g, '<br/>')
}

//var myUrl = basePath + 'JarpListener?className=cn.com.jsepc.midawa.utils.CommUtils&methodName=getTybmxx&paramCount=1&param_0=3';
//改变url参数中的值
function setUrlParam(thisUrl, key, value){
	var reg = new RegExp(key + "=[^\\s&#]*","gi");
    if (reg.test(thisUrl)){
       	thisUrl = thisUrl.replace(reg, key + "=" + value);
    }else{
        thisUrl += "&" + key + "=" + value;
    }   
	return thisUrl;
}
//alert(setUrlParam(myUrl, 'param_0', 'xuyan'));
var $=document.getElementById;